package com.cookor.poi.modules.phy.mobile;

import com.cookor.poi.common.persistence.Page;
import com.cookor.poi.common.utils.JedisUtils;
import com.cookor.poi.common.utils.ResultUtil;
import com.cookor.poi.modules.phy.entity.TArticle;
import com.cookor.poi.modules.phy.entity.TArticleType;
import com.cookor.poi.modules.phy.entity.TBannerPic;
import com.cookor.poi.modules.phy.service.TActionService;
import com.cookor.poi.modules.phy.service.TArticleService;
import com.cookor.poi.modules.phy.service.TArticleTypeService;
import com.cookor.poi.modules.phy.service.TBannerPicService;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping({"/api/phy/index"})
@ResponseBody
public class ApiIndexController extends ApiBaseController {
  @Autowired
  private TBannerPicService tBannerPicService;
  
  @Autowired
  private TActionService tActionService;
  
  @Autowired
  private TArticleTypeService tArticleTypeService;
  
  @Autowired
  private TArticleService tArticleService;
  
  @RequestMapping({"/data"})
  public ResultUtil indexData(HttpServletRequest request) {
    ResultUtil result = getResult();
    try {
      Integer tenantId = getTenantId(request);
      if (JedisUtils.getObject("bannerPics") != null) {
        result.putParams("bannerPics", JedisUtils.getObject("bannerPics"));
      } else {
        TBannerPic tBannerPic = new TBannerPic();
        tBannerPic.setTenantId(tenantId);
        tBannerPic.setDelFlag("0");
        List<TBannerPic> list = this.tBannerPicService.findList(tBannerPic);
        result.putParams("bannerPics", list);
        JedisUtils.setObject("bannerPics", list, 1800);
      } 
      TArticleType tArticleType = new TArticleType();
      Object articleList = JedisUtils.getObject("articleList");
      if (articleList != null) {
        List<TArticleType> typeList = (List<TArticleType>)articleList;
        result.putParams("articleList", typeList);
        if (!typeList.isEmpty()) {
          result.putParams("typeId", ((TArticleType)typeList.get(0)).getId());
          result.putParams("typeTitle", ((TArticleType)typeList.get(0)).getTypeName());
          result.putParams("articleListFirst", typeList.get(0));
        } 
      } else {
        tArticleType = new TArticleType();
        tArticleType.setTenantId(tenantId);
        tArticleType.setDelFlag("0");
        List<TArticleType> typeList = this.tArticleTypeService.findList(tArticleType);
        Iterator<TArticleType> it = typeList.iterator();
        while (it.hasNext()) {
          TArticleType type = it.next();
          if (type.getTypeName().equals("系统公告")) {
            it.remove();
            continue;
          } 
          TArticle tArticle = new TArticle();
          tArticle.setTenantId(tenantId);
          tArticle.setType(Integer.valueOf(type.getId()));
          tArticle.setDelFlag("0");
          type.setArticleList(this.tArticleService.findPage(new Page(1, 3), tArticle).getList());
        } 
        if (!typeList.isEmpty()) {
          result.putParams("typeId", ((TArticleType)typeList.get(0)).getId());
          result.putParams("typeTitle", ((TArticleType)typeList.get(0)).getTypeName());
          result.putParams("articleListFirst", typeList.get(0));
        } 
        result.putParams("articleList", typeList);
        JedisUtils.setObject("articleList", typeList, 1800);
      } 
    } catch (Exception e) {
      result.setCode(ResultUtil.ERROR_CODE);
      result.setError(e.getMessage());
      this.logger.error("WxIndexController data error!", e);
    } 
    return result;
  }
  
  @RequestMapping({"/getArticleList"})
  public ResultUtil getArticleList(String typeId, HttpServletRequest request) {
    ResultUtil result = getResult();
    try {
      Integer tenantId = getTenantId(request);
      TArticle tArticle = new TArticle();
      tArticle.setTenantId(tenantId);
      tArticle.setType(Integer.valueOf(typeId));
      tArticle.setDelFlag("0");
      List<TArticle> list = this.tArticleService.findPage(new Page(1, 3), tArticle).getList();
      result.putParams("articleListFirst", list);
    } catch (Exception e) {
      result.setCode(ResultUtil.ERROR_CODE);
      result.setError(e.getMessage());
      this.logger.error("WxIndexController getArticleList error!", e);
    } 
    return result;
  }
}
